package com.letao.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letao.server.dto.PageDTO;
import com.letao.server.dto.Result;
import com.letao.server.dto.SkuCommentDTO;
import com.letao.server.entity.SkuCommentInfo;
import com.letao.server.entity.SkuInfo;
import com.letao.server.entity.UserInfo;
import com.letao.server.mapper.SkuCommentInfoMapper;
import com.letao.server.service.ISkuCommentService;
import com.letao.server.service.ISkuService;
import com.letao.server.service.ISpuService;
import com.letao.server.service.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SkuCommentServiceImpl extends ServiceImpl<SkuCommentInfoMapper, SkuCommentInfo> implements ISkuCommentService {
    @Resource
    private SkuCommentInfoMapper mapper;

    @Resource
    private IUserService userService;

    @Resource
    private ISkuService skuService;

    @Resource
    private ISpuService spuService;

    /**
     * 查询评论
     *
     * @param spuId    spu的id
     * @param page     页码
     * @param pageSize 页大小
     * @return 返回的结果
     */
    @Override
    public Result getComment(Long spuId, Long page, Long pageSize) {
        IPage<SkuCommentInfo> ipage = new Page<>();
        QueryWrapper<SkuCommentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id", spuId);
        wrapper.orderByDesc("create_time");
        IPage<SkuCommentInfo> iPage = mapper.selectPage(ipage, wrapper);
        List<SkuCommentInfo> list = iPage.getRecords();
        List<SkuCommentDTO> ulist = new ArrayList<>();
        for (SkuCommentInfo info : list) {
            SkuCommentDTO skuCommentDTO = BeanUtil.copyProperties(info, SkuCommentDTO.class);
            UserInfo user = userService.getById(info.getUserId());
            skuCommentDTO.setUserName(user.getAccount());
            skuCommentDTO.setUserImage(user.getUserImage());
            SkuInfo sku = skuService.getById(info.getSkuId());
            skuCommentDTO.setSkuName(sku.getSkuName());
            skuCommentDTO.setSpuName(spuService.getById(sku.getSpuId()).getSpuName());
            ulist.add(skuCommentDTO);
        }
        PageDTO<SkuCommentDTO> dto = new PageDTO<>();
        dto.setPageSize(pageSize);
        dto.setCurrentPage(page);
        dto.setRows(ulist);
        dto.setTotalSize(iPage.getTotal());
        return Result.success(dto);
    }

    @Override
    public Result getCommentSku(Long id) {
        QueryWrapper<SkuCommentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", id);
        wrapper.orderByDesc("create_time");
        List<SkuCommentInfo> skuCommentInfoList = mapper.selectList(wrapper);
        List<SkuCommentDTO> ulist = new ArrayList<>();
        for (SkuCommentInfo info : skuCommentInfoList) {
            SkuCommentDTO skuCommentDTO = BeanUtil.copyProperties(info, SkuCommentDTO.class);
            UserInfo user = userService.getById(info.getUserId());
            skuCommentDTO.setUserName(user.getAccount());
            skuCommentDTO.setUserImage(user.getUserImage());
            SkuInfo sku = skuService.getById(info.getSkuId());
            skuCommentDTO.setSkuName(sku.getSkuName());
            skuCommentDTO.setSpuName(spuService.getById(sku.getSpuId()).getSpuName());
            ulist.add(skuCommentDTO);
        }
        return Result.success(ulist);
    }

    @Override
    public Result getOrderComment(Long orderid,Long skuId) {
        QueryWrapper<SkuCommentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderid);
        wrapper.orderByDesc("create_time");
        if(skuId!=null) {
            wrapper.eq("sku_id", skuId);
        }
        List<SkuCommentInfo> skuCommentInfoList = mapper.selectList(wrapper);
        List<SkuCommentDTO> ulist = new ArrayList<>();
        for (SkuCommentInfo info : skuCommentInfoList) {
            SkuCommentDTO skuCommentDTO = BeanUtil.copyProperties(info, SkuCommentDTO.class);
            UserInfo user = userService.getById(info.getUserId());
            skuCommentDTO.setUserName(user.getAccount());
            skuCommentDTO.setUserImage(user.getUserImage());
            SkuInfo sku = skuService.getById(info.getSkuId());
            skuCommentDTO.setSkuName(sku.getSkuName());
            skuCommentDTO.setSpuName(spuService.getById(sku.getSpuId()).getSpuName());
            ulist.add(skuCommentDTO);
        }
        if(skuId!=null&&ulist.size()>0)return Result.success(ulist.get(0));
        else if(skuId!=null && ulist.size()==0)return Result.success();
        return Result.success(ulist);
    }
}
